package com.happyscan138.makefun;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.happyscan138.makefun.Urls.Urls;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/9.
 */

public class forgetPassword extends AppCompatActivity implements View.OnClickListener {
    private ImageButton back;
    private Button save;
    private Button reg;
    private EditText editTextTel,editTextReg,editTextPas,editTextEnt;
    private String telephone,code,password,enterPassword;
    private String msg;
    private boolean runningThree;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forget_password);
        initView();
    }
    public void initView(){
        TextChange change=new TextChange();
        back=(ImageButton)findViewById(R.id.forgetPassword_back);
        back.setOnClickListener(this);
        save=(Button)findViewById(R.id.forgetPassword_save);
        save.setOnClickListener(this);
        editTextTel=(EditText)findViewById(R.id.forgetPassword_TEL);
        editTextTel.addTextChangedListener(change);
        editTextReg=(EditText)findViewById(R.id.forgetPassword_Reg);
        editTextReg.addTextChangedListener(change);
        editTextPas=(EditText)findViewById(R.id.forgetPassword_Pas);
        editTextPas.addTextChangedListener(change);
        editTextEnt=(EditText)findViewById(R.id.forgetPassword_Ent);
        editTextEnt.addTextChangedListener(change);
        reg=(Button)findViewById(R.id.forgetPassword_Rec);
        reg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.forgetPassword_back:
                finish();
                break;
            case R.id.forgetPassword_Rec:
                if(telephone==null){
                    Toast.makeText(forgetPassword.this,"请输入手机号",Toast.LENGTH_SHORT).show();
                }else {
                params.put("phone_no",telephone);
                RequestHandle handle=client.post(Urls.returnPassword_Code,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr = response.toString();
                        log.e("jsonStr", jsonStr);
                        try {
                            JSONObject jsonObject = new JSONObject(jsonStr);
                            msg = jsonObject.getString("msg");
                            Toast.makeText(forgetPassword.this, msg, Toast.LENGTH_SHORT).show();
                            JSONObject data = jsonObject.getJSONObject("data");
                            String out_time = data.getString("out_time");
                            log.e("out_time", out_time);
                            int time = Integer.valueOf(out_time).intValue();
                            CountDownTimer downTimer = new CountDownTimer(time * 1000, 1000) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    runningThree = true;
                                    reg.setText((millisUntilFinished / 1000) + "秒之后重新发送");
                                }

                                @Override
                                public void onFinish() {
                                    runningThree = false;
                                    reg.setText("重新发送");
                                }
                            };
                            if (runningThree) {
                            } else {
                                downTimer.start();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }
            case R.id.forgetPassword_save:
                params.put("phone_no",telephone);
                params.put("rand_code",code);
                params.put("new_pwd",password);
                params.put("confirm_pwd",enterPassword);
                Log.e("params",params.toString());
                RequestHandle handle=client.post(Urls.ForgetPassword,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            msg=jsonObject.getString("msg");
                            Toast.makeText(forgetPassword.this,msg,Toast.LENGTH_SHORT).show();
                            int i=jsonObject.getInt("code");
                            if(i==1){
                                startActivity(new Intent(forgetPassword.this,MainActivity.class));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }
    }
    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            telephone=editTextTel.getText().toString();
            code=editTextReg.getText().toString();
            password=editTextPas.getText().toString();
            enterPassword=editTextEnt.getText().toString();


        }
    }  //EditText的监听器
}
